import { Component, Input } from '@angular/core';
import { CascaderItem } from 'ng-devui';
import { PutDepartment } from 'src/app/@core/data/Department';
@Component({
  selector: 'app-edit-department',
  templateUrl: './edit-department.component.html',
  styleUrls: ['./edit-department.component.scss']
})
export class EditDepartmentComponent {
  @Input() data: any;
  options :any;//部门
  PutDepartment: PutDepartment={
    name: '',
    note: '',
    parentDepartmentId: null,
    departmentCode: '',
    enable: false
  };//新增部门
  items:any[]=[
    '启用', '禁用'
  ];
  currentSelectedItem = '启用';
  ngOnInit(): void {
    this.PutDepartment.name = this.data.option.name
    this.PutDepartment.note = this.data.option.note
    this.PutDepartment.departmentCode = this.data.option.departmentCode
    this.PutDepartment.enable = this.data.option.enable
    this.currentSelectedItem = this.PutDepartment.enable == true ? '启用' : '禁用'
    this.options = this.convertToCascaderItem(this.data.options)
    if(this.data.pathPids.legth>1){
      this.PutDepartment.parentDepartmentId = this.data.pathPids[this.data.pathPids.length-1]
    }
  }
  convertToCascaderItem(data: any[]): CascaderItem[] {
    return data.map(item => {
      let cascaderItem: CascaderItem = {
        label: item.name,
        value: item.departmentId,
        icon: item.icon
      };
      if (item.subCategories) {
        cascaderItem.children = this.convertToCascaderItem(item.subCategories);
      }
      return cascaderItem;
    });
  }//转换数据格式为华为库练级菜单的数据格式
  onChanges($event: string | any[]){
    this.PutDepartment.parentDepartmentId = $event[$event.length-1]
  }
  valueChange(value: string): void {
    this.PutDepartment.enable = value == '启用' ? true : false
  }
}
